package pachong;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;

import weibo4j.Timeline;
import weibo4j.model.Paging;
import weibo4j.model.RateLimitStatus;
import weibo4j.model.Status;
import weibo4j.model.StatusWapper;
import weibo4j.model.WeiboException;

public class get_weibo {

	/**
	 * @param args
	 * @throws IOException
	 * @throws ClassNotFoundException
	 * @throws SQLException
	 *             args[0] = acccess_token args[1] is the screen_name of the
	 *             user who do you want to get the tag of.
	 */
	String MYSQL_URL;
	String MYSQL_ACCOUNT;
	String MYSQL_PIN;
	String access_token;
	String screen_name;
	String is_clear;

	void set_config(String[] weibo_args) {
		BufferedReader input_config;

		access_token = weibo_args[0];
		screen_name = weibo_args[1];
		is_clear = weibo_args[2];
		try {
			input_config = new BufferedReader(new InputStreamReader(
					new FileInputStream("config.txt")));
			MYSQL_URL = input_config.readLine();
			MYSQL_ACCOUNT = input_config.readLine();
			MYSQL_PIN = input_config.readLine();
			if (MYSQL_PIN == null)
				MYSQL_PIN = "";
		} catch (IOException e) {
			System.out.println("Error: Can not open config file!");
		}
	}
	public void solve(String[] weibo_args, RateLimitStatus limitStatus)
			throws ClassNotFoundException, SQLException, WeiboException {

		set_config(weibo_args);

		// 驱动数据库连接
		Connection conn;
		Class.forName("com.mysql.jdbc.Driver");

		conn = DriverManager.getConnection("jdbc:" + MYSQL_URL
				+ "?characterEncoding=gbk", MYSQL_ACCOUNT, MYSQL_PIN);

		// 清空表单
		Statement state = conn.createStatement();
		if (is_clear == "Y") {
			state.executeUpdate("TRUNCATE TABLE  `weibos`");
		}

		// Declar a TIMELINE object and send the access_token to it
		Timeline tm = new Timeline();
		tm.client.setToken(access_token);

		// Paging is an object which is used to set the number of current
		// page and the count WEIBO number of one page.
		int page_count = 50;
		Paging page = new Paging(1);
		page.setCount(page_count);

		// StatusWapper is class which is used to describe the feather of
		// user's TIMELINE.

		StatusWapper status = tm.getUserTimelineByName(screen_name, page, 0, 0);

		// total number of this user's WEIBO.
		long num_weibo = status.getTotalNumber();
		if (num_weibo >= 1000)
			num_weibo = 1000;

		// total page number of this user's WEIBO.
		int page_num = (int) (num_weibo / page_count);
		if (0 != (num_weibo % page_count))
			page_num++;

		Status temp = new Status();

		/*
		 * 数据库表单名称为weibos， 含有五项内容 一、用户微博昵称 二、微博内容 三、转自微博内容 四、转发数 五、评论数
		 */
		PreparedStatement Statement = conn
				.prepareStatement("INSERT INTO  `weibo_collection`.`weibos` (`screen_name` ,`text` ,`source` ,`repostsCount` ,`commentsCount`) VALUES(?,?,?,?,?)");

		// 分页获取微博
		for (int i = 1; i <= page_num; i++) {
			page.setPage(i);
			status = tm.getUserTimelineByName(screen_name, page, 0, 0);

			// 抓取本页微博
			for (Status s : status.getStatuses()) {
				System.out.println(limitStatus.toString());
				Statement.setString(1, screen_name);
				Statement.setString(2, s.getText());

				temp = s.getRetweetedStatus();
				if (temp != null)
					Statement.setString(3, temp.getText());
				else
					Statement.setString(3, "");

				Statement.setString(4, String.valueOf(s.getRepostsCount()));
				Statement.setString(5, String.valueOf(s.getCommentsCount()));
				Statement.executeUpdate();

			}
		}

	}

}
